package securityproject;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import securityproject.utils.RuleUtils;

public class RuleFromLog extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1828537069582043887L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		HashMap<String, String> filters = new HashMap<String, String>();

		// Enumeration<?> params2 =req.getAttributeNames();
		Enumeration<?> params = req.getParameterNames();

		while (params.hasMoreElements()) {

			Object elem = params.nextElement();
			if (elem != null)
				filters.put(elem.toString(), req.getParameter(elem.toString()));

		}

		String ruleName;

		try {

			ruleName = "tmpRuleFromLog";

			DocumentBuilder dBuilder;
			// Instantiate a DocumentBuilderFactory.
			javax.xml.parsers.DocumentBuilderFactory dfactory = DocumentBuilderFactory
					.newInstance();
			dBuilder = dfactory.newDocumentBuilder();

			Document document = dBuilder.newDocument();

			// create base elemnt

			Element rule = document.createElement("rule");
			document.appendChild(rule);

			// set rule name

			Element name = document.createElement("name");
			rule.appendChild(name);

			Text nameText = document.createTextNode("");
			name.appendChild(nameText);

			// set rule description

			Element description = document.createElement("description");
			rule.appendChild(description);

			Text descriptionText = document.createTextNode("");
			description.appendChild(descriptionText);

			// set rule scope

			Element ruleScope = document.createElement("ruleScope");
			rule.appendChild(ruleScope);

			// ruleScopeParam = req.getParameter("ruleScope");
			Text ruleScopeText = document.createTextNode("global");
			ruleScope.appendChild(ruleScopeText);

			// set rule type

			Element ruleType = document.createElement("ruleType");
			rule.appendChild(ruleType);

			// ruleTypeParam = req.getParameter("ruleType");
			Text ruleTypeText = document.createTextNode("normal");
			ruleType.appendChild(ruleTypeText);

			// set rule filter type

			Element filterType = document.createElement("filterType");
			rule.appendChild(filterType);

			// filterTypeParam = req.getParameter("filterType");
			Text filterTypeText = document.createTextNode("exclude");
			filterType.appendChild(filterTypeText);

			// set rule tag name

			Element tagName = document.createElement("tagName");
			rule.appendChild(tagName);

			// String tagNameParam = req.getParameter("tagName");
			// if (tagNameParam == null) {
			// tagNameParam = "";
			// }

			Text tagNameText = document.createTextNode("");
			tagName.appendChild(tagNameText);

			// add filters

			for (Map.Entry<String, String> filterEntry : filters.entrySet()) {
				// System.out.println(filterEntry.getKey() + " age " +
				// filterEntry.getValue());
				Element filter = document.createElement("filter");
				rule.appendChild(filter);

				// add filter field name

				Element fieldName = document.createElement("fieldName");
				filter.appendChild(fieldName);

				Text fieldNameText = document.createTextNode(filterEntry
						.getKey());
				fieldName.appendChild(fieldNameText);

				// add expression value

				Element expression = document.createElement("expression");
				filter.appendChild(expression);

				Text expressionText = document.createTextNode(filterEntry
						.getValue());
				expression.appendChild(expressionText);

			}

			RuleUtils.writeXML(ruleName, document);

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//TODO call presentRule for editing rule, also remember to rename the xml file name 
		PresentRule pr = new PresentRule();
//		HttpServletRequest myReq = new HttpServletRequestWrapper(req);
//		myReq.getParameterMap().put(PresentRuleConstants.RULE_NAME, "tmpRuleFromLog");
		
		
		pr.init(resp, null, "tmpRuleFromLog");
		
		String rule =  RuleUtils.getXmlRule("tmpRuleFromLog");
		File ruleFile = new File(rule);
		if (ruleFile.exists()) {
			ruleFile.delete();
		}
	}

	
}

// String data = (String) req.getParameter("data");
// String httpAccept = (String) req.getParameter("http.accept");
// String httpAccept_encoding = (String) req
// .getParameter("http.accept_encoding");
// String httpConnection = (String) req.getParameter("http.connection");
// String httpContentLengthHeader = (String) req
// .getParameter("http.content_length_header");
// String httpContent_type = (String) req
// .getParameter("http.content_type");
// String httpCookie = (String) req.getParameter("http.cookie");
// String httpHost = (String) req.getParameter("http.host");
// String httpRequest = (String) req.getParameter("http.request");
// String httpRequestMethod = (String) req
// .getParameter("http.request.method");
// String httpRequestUri = (String) req.getParameter("http.request.uri");
// String httpRequestVersion = (String) req
// .getParameter("http.request.version");
// String httpUser_agent = (String) req.getParameter("http.user_agent");
//
// String httpAccept_language = (String) req
// .getParameter("http.accept_language");
// String httpAuthbasic = (String) req.getParameter("http.authbasic");
// String httpAuthorization = (String) req
// .getParameter("http.authorization");
// String httpCache_control = (String) req
// .getParameter("http.cache_control");
// String httpContent_encoding = (String) req
// .getParameter("http.content_encoding");
// String httpContent_length = (String) req
// .getParameter("http.content_length");
// String httpDate = (String) req.getParameter("http.date");
// String httpLast_modified = (String) req
// .getParameter("http.last_modified");
// String httpLocation = (String) req.getParameter("http.location");
// String httpNotification = (String) req
// .getParameter("http.notification");
// String httpProxy_authenticate = (String) req
// .getParameter("http.proxy_authenticate");
// String httpProxy_authorization = (String) req
// .getParameter("http.proxy_authorization");
// String httpProxy_connect_host = (String) req
// .getParameter("http.proxy_connect_host");
// String httpProxy_connect_port = (String) req
// .getParameter("http.proxy_connect_port");
// String httpReferer = (String) req.getParameter("http.referer");
// String httpResponse = (String) req.getParameter("http.response");
// String httpResponseCode = (String) req
// .getParameter("http.response.code");
// String httpServer = (String) req.getParameter("http.server");
// String httpSet_cookie = (String) req.getParameter("http.set_cookie");
// String httpTransfer_encoding = (String) req
// .getParameter("http.transfer_encoding");
// String httpWww_authenticate = (String) req
// .getParameter("http.www_authenticate");
// String httpX_forwarded_for = (String) req
// .getParameter("http.x_forwarded_for");

// http://www.wireshark.org/docs/dfref/h/http.html